library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(p8105.datasets)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data(instacart)

instacart = 
  instacart %>% 
  as_tibble(instacart)

instacart
## # A tibble: 1,384,617 x 15
##    order_id product_id add_to_cart_order reordered user_id eval_set order_number
##       <int>      <int>             <int>     <int>   <int> <chr>           <int>
##  1        1      49302                 1         1  112108 train               4
##  2        1      11109                 2         1  112108 train               4
##  3        1      10246                 3         0  112108 train               4
##  4        1      49683                 4         0  112108 train               4
##  5        1      43633                 5         1  112108 train               4
##  6        1      13176                 6         0  112108 train               4
##  7        1      47209                 7         0  112108 train               4
##  8        1      22035                 8         1  112108 train               4
##  9       36      39612                 1         0   79431 train              23
## 10       36      19660                 2         1   79431 train              23
## # ... with 1,384,607 more rows, and 8 more variables: order_dow <int>,
## #   order_hour_of_day <int>, days_since_prior_order <int>, product_name <chr>,
## #   aisle_id <int>, department_id <int>, aisle <chr>, department <chr>
insta_bar =
instacart %>% 
  count(department) %>% 
  filter(!is.na(department)) %>% 
  mutate(department = fct_reorder(department, n)) %>% 
  plot_ly(x = ~department, y = ~n, color = ~department, type = "bar", colors = "viridis")

insta_bar
insta_box = 
  instacart %>% 
  filter(!is.na(department)) %>% 
  mutate(department = fct_reorder(department, order_hour_of_day)) %>% 
  ggplot(aes(x = department, y = order_hour_of_day, fill = department)) +
  geom_boxplot() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplotly(insta_box)
insta_scatter = 
  instacart %>% 
  count(days_since_prior_order) %>% 
  filter(!is.na(days_since_prior_order)) %>% 
  ggplot(aes(x = days_since_prior_order, y = n, color = n)) +
  geom_point(alpha = 0.25) +
  coord_cartesian()

ggplotly(insta_scatter)